package com.guochu.org.service.impl;

import com.guochu.common.infrastructure.IService;
import com.guochu.common.infrastructure.Identity;
import com.guochu.org.dao.UserDao;
import com.guochu.org.entity.User;
import com.guochu.org.service.UserService;
import com.guochu.utils.Pagination;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 用户管理表(User)表服务实现类
 *
 * @author bunting.wei
 * @since 2020-12-11 15:36:19
 */
@Service
public class UserServiceImpl extends IService implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(User user) {
        user.setId(UUID.randomUUID().toString());
        user.setCreateDate(LocalDateTime.now());
        user.setDeleteMark(0);
        user.setEnabledMark(1);
        return this.userDao.insert(user);
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public int update(User user) {
        return this.userDao.update(user);
    }

    /**
     * 逻辑删除多条记录
     *
     * @param limit 主键
     * @return
     */
    @Override
    public int removeById(Map<String, Object> limit) {
        return this.userDao.removeById(limit);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(String id) {
        return this.userDao.getById(id);
    }

    /**
     * 根据条件查询分页数据
     *
     * @param pagination 分页条件
     * @param limit      查询条件
     * @return 分页数据
     */
    public List<User> queryPageByLimit(Pagination pagination, Map<String, Object> limit) {
        pagination.setTotal(this.userDao.recordCount(limit));
        limit.put("offset", (pagination.getCurrentPage() - 1) * pagination.getPageSize());
        limit.put("pageSize", pagination.getPageSize());
        return this.userDao.queryPageByLimit(limit);
    }

    /**
     * 根据条件查询全部数据
     *
     * @param limit 查询条件
     * @return 所有数据
     */
    @Override
    public List<User> queryAllByLimit(Map<String, Object> limit) {
        return this.userDao.queryAllByLimit(limit);
    }
}
